<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Quizzes_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('quiz_model', '', TRUE);
        $this->load->model('group_model', '', TRUE);
        if (!$this->session->userdata('logged_in')) {
            redirect('login');
        }

        $this->load->library('authentication');
        $this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();
        $this->authentication->redirectIfNotBe(array('HR', 'SC'));
    }

    function index($limit = '0') {
        //$logged_in = $this->session->userdata('logged_in');
        //if ($logged_in['su'] == "1") {
        $viewData['result'] = $this->quiz_model->quiz_list($limit);
        //} else {
        //$gid = $logged_in['gid'];
        //$data['result'] = $this->quiz_model->quiz_list($limit, $gid);
        //}
        $viewData['title'] = "Quiz/Test";
        $viewData['limit'] = $limit;

        if (isset($_SESSION['message'])) {
            $viewData['message'] = $_SESSION['message'];
            $viewData['messageType'] = $_SESSION['messageType'];
            unset($_SESSION['message']);
            unset($_SESSION['messageType']);
        }

        $this->load->view(get_folder($this->loggedUser->title) . '/quizzes_list', $viewData);
    }

    function add_new() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->load->model('category', '', TRUE);
        $viewData['category'] = $this->category->category_dropdown();

        $this->load->model('difficult_level', '', TRUE);
        $viewData['groups'] = $this->group_model->get_allgroups();

        $this->load->model('evaluation', '', TRUE);
        $e = $this->evaluation->get_last();
        $viewData['eid'] = $e['id'];

        $viewData['difficult_level'] = $this->difficult_level->level_dropdown();
        if ($this->input->post('submit_quiz')) {
            // thực hiện validation
            $this->form_validation->set_rules('quiz_name', 'Tên trắc nghiệm', 'required');
            $this->form_validation->set_rules('quiz_time_duration', 'Thời gian làm bài', 'required|is_natural_no_zero');
            $this->form_validation->set_rules('test_start_time', 'Thời điểm bắt đầu làm bài', 'required');
            $this->form_validation->set_rules('test_end_time', 'Thời điểm hết hạn làm bài', 'required');
            $this->form_validation->set_rules('cid', 'Số câu hỏi', 'required|greater_than[0]');
            if ($this->input->post('cid') > 0) {
                $this->form_validation->set_rules('did', 'Số câu hỏi', 'required|greater_than[0]');
            }

            $this->form_validation->set_message('greater_than', 'Chưa chọn %s');


            if ($this->form_validation->run() == false) {
                $viewData['messageType'] = 'danger';
            } else {
                // kiểm tra số câu hỏi ở đây
                $noq = $this->input->post('no_of_questions');
                if ($this->input->post('cid') > 0 && $this->input->post('did') > 0 && intval($noq[0]) == 0) {
                    $viewData['message'] = 'Chưa chọn Số câu hỏi';
                    $viewData['messageType'] = 'danger';
                } else {
                    $_SESSION['message'] = $this->quiz_model->add_quiz();
                    $_SESSION['messageType'] = 'success';
                    redirect(get_folder($this->loggedUser->title) . '/quizzes');
                    return;
                }
            }
        }
        $viewData['title'] = "Add new quiz";

        $this->load->view(get_folder($this->loggedUser->title) . '/quiz_add_new', $viewData);
    }

    function edit_quiz($id) {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->load->model('evaluation', '', TRUE);
        $e = $this->evaluation->get_last();
        $viewData['eid'] = $e['id'];

        $this->load->model('category', '', TRUE);
        $viewData['category'] = $this->category->category_dropdown();
        $this->load->model('difficult_level', '', TRUE);
        $viewData['groups'] = $this->group_model->get_allgroups();

        $viewData['difficult_level'] = $this->difficult_level->level_dropdown();
        if ($this->input->post('submit_quiz')) {
            // thực hiện validation
            $this->form_validation->set_rules('quiz_name', 'Tên trắc nghiệm', 'required');
            $this->form_validation->set_rules('quiz_time_duration', 'Thời gian làm bài', 'required|is_natural_no_zero');
            $this->form_validation->set_rules('test_start_time', 'Thời điểm bắt đầu làm bài', 'required');
            $this->form_validation->set_rules('test_end_time', 'Thời điểm hết hạn làm bài', 'required');
//            $this->form_validation->set_rules('cid', 'Số câu hỏi', 'required|greater_than[0]');
//            if ($this->input->post('cid') > 0) {
//                $this->form_validation->set_rules('did', 'Số câu hỏi', 'required|greater_than[0]');
//            }

            $this->form_validation->set_message('greater_than', 'Chưa chọn %s');

            if ($this->form_validation->run() == false) {
                $viewData['messageType'] = 'danger';
            } else {
                // kiểm tra số câu hỏi ở đây
                // tất cả mà đều bằng 0 -> tạch <=> chỉ cần 1 đứa khác 0 là ok
                $noq = $this->input->post('no_of_questions');
                $ok = false;
                foreach ($noq as $value) {
                    if(intval($value) > 0){
                        $ok = true;
                    }
                }
                if ($ok == false) {
                    $viewData['message'] = 'Chưa chọn Số câu hỏi';
                    $viewData['messageType'] = 'danger';
                } else {
                    $viewData['message'] = $this->quiz_model->edit_quiz($id);
                    $viewData['messageType'] = 'success';
                }
            }


            //$viewData['resultstatus'] = 
        }
        $viewData['result'] = $this->quiz_model->quiz_detail($id);
        $viewData['assigned_gids'] = $this->quiz_model->assigned_groups($id);
        $viewData['assigned_questions'] = $this->quiz_model->assigned_questions($id);

        $viewData['title'] = "Edit quiz";

        $this->load->view(get_folder($this->loggedUser->title) . '/quiz_edit', $viewData);
    }

    function remove_quiz($id) {
//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }

        $viewData['resultstatus'] = $this->quiz_model->remove_quiz($id);
        redirect(get_folder($this->loggedUser->title) . '/quizzes', 'refresh');
    }

    function attempt($id) {
        $quiz = $this->quiz_model->quiz_detail($id);
        if ($quiz != false) {
            $viewData['result'] = $quiz;
            $viewData['title'] = $viewData['result']->quiz_name;
            $this->load->view(get_folder($this->loggedUser->title) . '/quiz_before_attempt', $viewData);
        } else {
            show_404();
        }
    }

    function copy($id) {
        $quiz = $this->quiz_model->quiz_detail($id); //var_dump($quiz);exit;
        if ($quiz != false) {
            $this->load->model('evaluation', '', TRUE);
            $e = $this->evaluation->get_last();

            $quiz->quid = null;
            $quiz->quiz_name = 'Copy - ' . $quiz->quiz_name;
            $quiz->test_type = 0;
            $quiz->max_attempts = 1;
            $quiz->eid = $e['id'];

            $quiz->assigned_groups = $this->quiz_model->assigned_groups($id);
            $quiz->no_of_questions = $this->quiz_model->assigned_questions($id);
            //var_dump($quiz);exit;
            $this->quiz_model->insert($quiz);

            //$this->load->view(get_folder($this->loggedUser->title).'/quizzes_list', $viewData);
            redirect(get_folder($this->loggedUser->title) . '/quizzes', 'refresh');
        } else {
            
        }
    }

    // update individual question time spent
    function update_time($id, $qtime) {
        $this->quiz_model->update_time($id, $qtime);
    }

    // update answer
    function update_answer($id, $oid) {
        $this->quiz_model->update_answer($id, $oid);
    }

    function access_test($id) {
        $this->load->helper('cookie');

        $viewData['error'] = false;
        $viewData['message'] = $this->quiz_model->quiz_verify($id); // cookie dc tạo trong function này
        $viewData['quiz_id'] = $id;
        $viewData['result'] = $this->quiz_model->quiz_detail($id);
        $viewData['title'] = $viewData['result']->quiz_name;
        if ($viewData['message'] == "1") {
            if (!$this->input->cookie('rid', TRUE)) {
                redirect(get_folder($this->loggedUser->title) . '/quizzes/access_test/' . $id, 'refresh');
            }
            $rid = $this->input->cookie('rid', TRUE);

            // get assignied questions
            $viewData['assigned_question'] = $this->quiz_model->get_question($rid);
            // get time information
            $viewData['time_info'] = $this->quiz_model->get_time_info($rid);
            // time remaining in seconds
            // total time - time spent
            $viewData['seconds'] = (($viewData['result']->duration) * 60) - ($viewData['time_info']['time_spent']);

            // get quiz data like quiz duration, title
            $viewData['quiz_data'] = $this->quiz_model->get_quiz_data($id);

            // load quiz access page
            $this->load->view(get_folder($this->loggedUser->title) . '/quiz_access', $viewData);
        } else {
            $viewData['error'] = true;
            // load quiz detail page with error
            $viewData['result'] = $this->quiz_model->quiz_detail($id);
            $viewData['title'] = $viewData['result']->quiz_name;
            //$this->load->view('header', $data);
            $this->load->view(get_folder($this->loggedUser->title) . '/quiz_before_attempt', $viewData);
            //$this->load->view('footer', $data);
        }
    }

    function submit_quiz($id) {

        $this->load->helper('cookie');

        $viewData['resultstatus'] = $this->quiz_model->quiz_submit($id);
        $viewData['result'] = $this->quiz_model->quiz_detail($id);
        $viewData['title'] = $viewData['result']->quiz_name;

//        $this->load->view('header', $viewData);
//        $this->load->view('quiz_detail', $viewData);
//        $this->load->view('footer', $viewData);

        $rid = $this->input->cookie('rid', TRUE);

        redirect(get_folder($this->loggedUser->title) . '/results/view/' . $rid . '/' . $id);
    }

}

?>
